package cn.com.shopec.erp.multiChannel.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.multiChannel.dao.IProductPackageDao;
import cn.com.shopec.erp.multiChannel.entity.ProductPackage;
import cn.com.shopec.erp.multiChannel.vo.ChannelOrderVo;

@Repository
public class ProductPackageDaoImpl extends HibernateEntityDao<ProductPackage> implements IProductPackageDao {

	@SuppressWarnings("unchecked")
	@Override
	public PageFinder<Map<String, Object>> productPackagePaged(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		String params = "";
		if (channelOrderVo != null) {
			if (StringUtil.isExist(channelOrderVo.getChannelOrderId())) {
				params += " AND pp.channel_order_id ='" + channelOrderVo.getChannelOrderId() + "'";
			}
			if (StringUtil.isExist(channelOrderVo.getProdPackageNo())) {
				params += " AND pp.prod_package_no ='" + channelOrderVo.getProdPackageNo() + "'";
			}
			if (StringUtil.isExist(channelOrderVo.getChannelOrderType())) {
				params += " AND co.channel_order_type =" + channelOrderVo.getChannelOrderType();
			}
			if (StringUtil.isExist(channelOrderVo.getOrderType())) {
				params += " AND co.order_type =" + channelOrderVo.getOrderType();
			}
			if (StringUtil.isExist(channelOrderVo.getDeliveryState())) {
				params += " AND pp.delivery_state =" + channelOrderVo.getDeliveryState();
			}

			if (StringUtil.isExist(channelOrderVo.getOrderStartTime())) {
				params += " AND pp.delivery_time >='" + channelOrderVo.getOrderStartTime() + "'";
			}
			if (StringUtil.isExist(channelOrderVo.getOrderEndTime())) {
				params += " AND pp.delivery_time <='" + channelOrderVo.getOrderEndTime() + "'";
			}
		}
		String sql = "from tbl_product_package pp,tbl_channel_order co,tbl_warehouse w "
				+ "where pp.channel_order_id = co.channel_order_id " + " and pp.wh_no = w.wh_no " + params;
		String countsql = "select count(*) " + sql;
		String orderBySql = "  ORDER BY IF(ISNULL(pp.delivery_time),0,1),delivery_time desc";
		String querysql = "select pp.id id,pp.channel_order_id channelOrderId,pp.delivery_state deliveryState,pp.delivery_time deliveryTime,"
				+ "pp.prod_no prodNo,pp.prod_package_no prodPackageNo,pp.wh_no whNo,pp.sup_no supNo,pp.operator_id operatorId,pp.create_time createTime,"
				+ "pp.state,co.channel_order_type channelOrderType,w.wh_name whName " + sql + params + orderBySql;

		BigInteger count = (BigInteger) this.createSQLQuery(countsql).list().get(0);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		PageFinder<Map<String, Object>> page = null;
		if(pageQuery != null){
			list = getSession().createSQLQuery(querysql)
					.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
					.setFirstResult((pageQuery.getPage() - 1) * pageQuery.getPageSize())
					.setMaxResults(pageQuery.getPageSize()).list();
			page = new PageFinder<Map<String, Object>>(pageQuery.getPage(), pageQuery.getPageSize(), count.intValue(), list);
		} else{
			list = (List<Map<String, Object>>) getSession().createSQLQuery(querysql)
					.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
			page = new PageFinder<Map<String, Object>>(1, count.intValue()==0?1:count.intValue(), count.intValue(), list);
		}
		return page;
	}

	@Override
	public int updateByProdPackageNo(ProductPackage productPackage) {
		String params = "";
		Map<String,Object> values =  new HashMap<String,Object>();
		if (LogicUtil.isNotNull(productPackage.getDeliveryState())) {
			if (StringUtil.isExist(params)) {
				params += ",";
			}
			params += "delivery_state= :eliveryState";
			values.put("eliveryState", productPackage.getDeliveryState());
		}
		
		if (LogicUtil.isNotNull(productPackage.getDeliveryTime())) {
			if (StringUtil.isExist(params)) {
				params += ",";
			}
			params += "delivery_time= :deliveryTime";
			values.put("deliveryTime", productPackage.getDeliveryTime());
		}
		
		if (LogicUtil.isNotNull(productPackage.getState())) {
			if (StringUtil.isExist(params)) {
				params += ",";
			}
			params += "state= :state";
			values.put("state", productPackage.getState());
		}
		
		if (StringUtil.isExist(params)) {
			String sql = "update tbl_product_package set " + params + " where prod_package_no= :prodPackageNo";
			values.put("prodPackageNo", productPackage.getProdPackageNo());
			return this.createSQLQuery(sql, values).executeUpdate();
		} else {
			return 0;
		}
	}
}
